package com.raspberry.nethelper;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import com.raspberry.nethelper.net.SocketHelper;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends Activity implements  Runnable{
    SocketHelper socketHelper;
    private SurfaceHolder holder;
    private Thread mythread;
    private Canvas canvas;
    URL videoUrl;
    private String url;
    private int w;
    private int h;
    HttpURLConnection conn;
    Bitmap bmp;
    private String ip;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = this.getIntent();
        ip= intent.getStringExtra("ip");
        socketHelper= SocketHelper.getInstance(ip, 10086);
        Button btn_left = (Button)findViewById(R.id.btn_left);
        Button btn_right = (Button)findViewById(R.id.btn_right);
        Button btn_faw = (Button)findViewById(R.id.btn_faw);
        Button btn_ret = (Button)findViewById(R.id.btn_back);
        Button btn_stop = (Button)findViewById(R.id.btn_stop);
        Button btn_fast = (Button)findViewById(R.id.btn_fast);
        Button btn_slow = (Button)findViewById(R.id.btn_slow);
        Button btn_camRight = (Button)findViewById(R.id.btn_camRight);
        Button btn_camLeft = (Button)findViewById(R.id.btn_camLeft);
        Button btn_camUp = (Button)findViewById(R.id.btn_camUp);
        Button btn_camDown = (Button)findViewById(R.id.btn_camDown);
        btn_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickLeft();
            }
        });
        btn_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickRight();
            }
        });
        btn_faw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickFaw();
            }
        });
        btn_ret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickBack();
            }
        });
        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickStop();
            }
        });
        btn_fast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickFast();
            }
        });
        btn_slow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickSlow();
            }
        });
        btn_camLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickCamLeft();
            }
        });
        btn_camUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickCamUp();
            }
        });
        btn_camDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickCamDown();
            }
        });
        btn_camRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickCamRight();
            }
        });
//        url = getIntent().getExtras().getString("CameraIp");
        w = getWindowManager().getDefaultDisplay().getWidth();
        h = getWindowManager().getDefaultDisplay().getHeight();

        SurfaceView surface = (SurfaceView)findViewById(R.id.surface);

        surface.setKeepScreenOn(true);// ������Ļ����
        mythread = new Thread(this);
        holder = surface.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // TODO Auto-generated method stub

            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                // TODO Auto-generated method stub
                mythread.start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                       int height) {
                // TODO Auto-generated method stub

            }
        });
    }
    private void clickLeft(){
        System.out.println("left");

        socketHelper.writeString("left");
    }
    private void clickCamLeft(){
        System.out.println("camLeft");

        socketHelper.writeString("camLeft");
    }
    private void clickCamRight(){
        System.out.println("camRight");

        socketHelper.writeString("camRight");
    }

    private void clickCamUp(){
        System.out.println("camUp");

        socketHelper.writeString("camUp");
    }

    private void clickCamDown(){
        System.out.println("camDown");

        socketHelper.writeString("camDown");
    }
    private void clickRight(){
        System.out.println("right");
        socketHelper.writeString("right");
    }
    private void clickFaw(){
        System.out.println("faw");
        socketHelper.writeString("faw");
    }
    private void clickBack(){
        System.out.println("ret");
        socketHelper.writeString("ret");
    }
    private void clickStop(){
        System.out.println("stop");
        socketHelper.writeString("stop");
    }
    private void clickFast(){
        System.out.println("fast");
        socketHelper.writeString("fast");
    }
    private void clickSlow(){
        System.out.println("slow");
        socketHelper.writeString("slow");
    }
    @Override
    protected void onDestroy() {
        socketHelper.close();
        super.onDestroy();
    }

    private void draw() {
        // TODO Auto-generated method stub
        try {
            InputStream inputstream = null;
            //����һ��URL����
			url = "http://192.168.31.54:8080/?action=snapshot";
            videoUrl=new URL(url);
            conn = (HttpURLConnection)videoUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            inputstream = conn.getInputStream();
            bmp = BitmapFactory.decodeStream(inputstream);
            canvas = holder.lockCanvas();
            canvas.drawColor(Color.WHITE);
            RectF rectf = new RectF(0, 0, w, h);
            canvas.drawBitmap(bmp, null, rectf, null);
            holder.unlockCanvasAndPost(canvas);
            conn.disconnect();
        } catch (Exception ex) {
        } finally {
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while(true){
            draw();
        }
    }
}
